
import Ability from "../ability.js";
import Member from "./member.js";




export default class ExpM extends Member {
	totalExp: number;

	constructor(host: Ability) {
		super(host);
	}

	/**该等级的目前经验值 */
	get exp(): number {
		return this.totalExp - minTotalExpOfLevel(this.level);
	}

	set exp(exp: number) {
		let lastExp = this.exp;
		this.totalExp += exp - lastExp;
	}

	get level() {
		return totalExp2Level(this.totalExp);
	}
	/**该等级的最大经验值 */
	get maxExp() {
		return maxExpOfLevel(this.level);
	}
}



/**
 * 
 * @param {number} level 
 * @returns 
 */
function maxExpOfLevel(level) {
	let maxExp = [10, 100, 150, 300, 500, 750, 1000, 1500, 2000, 3000];
	if (level >= maxExp.length) return 1e10;
	return maxExp[level];
}

function minTotalExpOfLevel(level) {
	let mte = 0;
	for (let i = 0; i < level; i++) {
		mte += maxExpOfLevel(i);
	}
	return mte;
}

/**
 * 通过总经验计算等级
 * @param {number} totalExp 
 * @returns 
 */
function totalExp2Level(totalExp) {
	let level = 0;
	for (let i = maxExpOfLevel(level); i <= totalExp; i += maxExpOfLevel(level)) {
		level++;
	}
	return level;
}